package net.wanho.shop.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.wanho.shop.dto.GoodsDTO;
import net.wanho.shop.dto.GoodsPicsAttrsDTO;
import net.wanho.shop.service.GoodsService;
import net.wanho.shop.vo.AjaxResult;
import net.wanho.shop.vo.PageVo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;


/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Api(tags = "09.商品表接口")
@RestController
@RequestMapping("/goods")
@RequiredArgsConstructor
public class GoodsController {

    private final GoodsService goodsService;

    @ApiOperation("商品表分页查询")
    @GetMapping
    public AjaxResult findPage(@ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
                               @ApiParam("页大小") @RequestParam(defaultValue = "5") Integer pageSize,
                               @ApiParam("查询条件") GoodsDTO goodsDTO) {
        PageVo pageVo = goodsService.find(pageNum, pageSize, goodsDTO);
        return AjaxResult.success(pageVo);
    }

    @ApiOperation("根据主键查询商品表")
    @GetMapping("/{goodsId}")
    public AjaxResult findById(@PathVariable Integer goodsId) {
        return AjaxResult.success(goodsService.findById(goodsId));
    }

    @ApiOperation("新增商品表")
    @PostMapping
    public AjaxResult add(@RequestBody GoodsPicsAttrsDTO goodsPicsAttrsDTO) {
        goodsService.addGoods(goodsPicsAttrsDTO);
        return AjaxResult.success();
    }

    @ApiOperation("修改商品表")
    @PutMapping("{goodsId}")
    public AjaxResult modify(@RequestBody GoodsDTO goodsDTO, @PathVariable Integer goodsId) {
        goodsDTO.setGoodsId(goodsId);
        goodsService.modify(goodsDTO);
        return AjaxResult.success();
    }

    @ApiOperation("根据主键删除商品表")
    @DeleteMapping("/{goodsId}")
    public AjaxResult removeById(@PathVariable Serializable goodsId) {
        goodsService.removeById(goodsId);
        return AjaxResult.success();
    }

    @ApiOperation("修改商品状态")
    @PutMapping("{goodsId}/{goodsState}")
    public AjaxResult updateById(@PathVariable Integer goodsId, @PathVariable Integer goodsState) {
        goodsService.updateByIdState(goodsId, goodsState);
        return AjaxResult.success();
    }

}
